package com.example.research.controller;

import com.example.research.model.Research;
import com.example.research.model.ResearchType;
import com.example.research.model.Teacher;
import com.example.research.service.ResearchService;
import com.example.research.service.ResearchTypeService;
import com.example.research.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;


import java.util.List;

@Controller
@RequestMapping("/researches")
public class ResearchController {
    @Autowired
    private ResearchService researchService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private ResearchTypeService researchTypeService;

    @GetMapping
    public String listResearches(Model model) {
        model.addAttribute("researches", researchService.findAll());
        return "researches/list";
    }

    @GetMapping("/add")
    public String showAddForm(Model model) {
        model.addAttribute("research", new Research());
        model.addAttribute("teachers", teacherService.findAll());
        model.addAttribute("researchTypes", researchTypeService.findAll());
        return "researches/add";
    }

    @PostMapping("/add")
    public String addResearch(@ModelAttribute Research research) {
        researchService.save(research);
        return "redirect:/researches";
    }

    @GetMapping("/edit/{id}")
    public String showEditForm(@PathVariable Integer id, Model model) {
        Research research = researchService.findById(id)
                .orElseThrow(() -> new IllegalArgumentException("无效的研究ID: " + id));
        model.addAttribute("research", research);
        model.addAttribute("teachers", teacherService.findAll());
        model.addAttribute("researchTypes", researchTypeService.findAll());
        return "researches/edit";
    }



    @PostMapping("/edit/{id}")
    public String updateResearch(@PathVariable Integer id, @ModelAttribute Research research) {
        research.setResearchId(id);
        researchService.save(research);
        return "redirect:/researches";
    }

    @GetMapping("/delete/{id}")
    public String deleteResearch(@PathVariable Integer id) {
        researchService.delete(id);
        return "redirect:/researches";
    }

    @GetMapping("/search")
    public String search(@RequestParam(required = false) String keyword, Model model) {
        if (keyword != null && !keyword.isEmpty()) {
            model.addAttribute("researches", researchService.search(keyword));
        } else {
            model.addAttribute("researches", researchService.findAll());
        }
        return "researches/list";
    }

    @GetMapping("/stats")
    public String showStats(Model model) {
        List<Teacher> teachers = teacherService.findAll();
        List<ResearchType> types = researchTypeService.findAll();
        model.addAttribute("teachers", teachers);
        model.addAttribute("types", types);
        return "researches/stats";
    }

    @GetMapping("/stats/data")
    @ResponseBody
    public Long getStatsData(@RequestParam Integer teacherId, @RequestParam Integer typeId) {
        return researchService.countByTeacherAndType(teacherId, typeId);
    }
}
